package com.rfid.project.web;

import java.text.DateFormat;
import java.util.List;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ui.Model;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.data.domain.Page;

import biz.appframe.PageJsonBean;

import com.rfid.project.model.OperationLog;
import com.rfid.project.service.OperationLogService;
import com.rfid.project.model.People;
import com.rfid.project.service.PeopleService;

@Controller
public class OperationLogController {

	private static final Logger logger = LoggerFactory
			.getLogger(OperationLogController.class);

	@Autowired
	private OperationLogService operationLogService;

	@Autowired
	private PeopleService peopleService;

	@RequestMapping(value = "/operationLogmgr")
	public String operationLogmgr(HttpServletRequest request,
			HttpServletResponse response, Model model) {
		List<People> persons = peopleService.findAll();
		model.addAttribute("persons", persons);

		List<People> proxyPersons = peopleService.findAll();
		model.addAttribute("proxyPersons", proxyPersons);
		return "operationLogmgr";
	}
	
	@RequestMapping(value = "/queryOperationLog")
	public String queryOperationLog(
			HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam(value = "operationType", defaultValue = "") String operationType,
			@RequestParam(value = "object", defaultValue = "") String object,
			@RequestParam(value = "startDatetime", defaultValue = "") Date startDatetime,
			@RequestParam(value = "endDatetime", defaultValue = "") Date endDatetime,
			@RequestParam(value = "operationDescription", defaultValue = "") String operationDescription,
			@RequestParam(value = "personId", defaultValue = "-1") Long personId,
			@RequestParam(value = "proxyPersonId", defaultValue = "-1") Long proxyPersonId,
			@RequestParam(value = "page", defaultValue = "1") Integer pageNumber,
			@RequestParam(value = "rows", defaultValue = "10") Integer pageSize,
			@RequestParam(value = "sort", defaultValue = "") String sort,
			@RequestParam(value = "order", defaultValue = "") String order,
			Model model) {
		Page<OperationLog> operationLogs = operationLogService.findAll(
				startDatetime, endDatetime, operationType, object,
				operationDescription, personId, proxyPersonId, pageNumber,
				pageSize, sort, order);
		JSONObject result = new JSONObject();
		result.put("total", operationLogs.getTotalElements());
		JSONArray models = new JSONArray();
		for (OperationLog item : operationLogs) {
			JSONObject mod = new JSONObject();
			wrapData(item, mod);

			models.add(mod);
		}
		result.put("rows", models);
		PageJsonBean.printObject(response, result);
		return null;
	}
	
	@RequestMapping(value = "/findOperationLog")
	public String findOperationLog(
			HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam(value = "id", defaultValue = "") Long id,
			Model model) {
		OperationLog operationLog = operationLogService.findOne(id);
		JSONObject result = new JSONObject();
		JSONObject mod = new JSONObject();
		wrapData(operationLog, mod);
		result.put("success", true);
		result.put("data", mod);
		PageJsonBean.printObject(response, result);
		return null;
	}

	private void wrapData(OperationLog item, JSONObject mod) {
		mod.put("id", item.getId());
		mod.put("personId", item.getPerson().getId());
		mod.put("personName", item.getPerson().getName());
		mod.put("proxyPersonId", item.getProxyPerson().getId());
		mod.put("proxyPersonName", item.getProxyPerson().getName());
		mod.put("operationType", item.getOperationType());
		mod.put("object", item.getObjectID());
		mod.put("operationDateTime", com.rfid.project.util.DateUtil
				.dateAllToString(item.getOperationDateTime()));
		mod.put("operationDescription", item.getOperationDescription());
	}

	@RequestMapping(value = "/editOperationLog", method = RequestMethod.POST)
	public String editOperationLog(HttpServletRequest request,
			HttpServletResponse response, @Valid OperationLog operationLog,
			Model model, RedirectAttributes redirectAttributes) {
		operationLogService.saveOperationLog(operationLog);

		JSONObject result = new JSONObject();
		result.put("success", true);
		PageJsonBean.printObject(response, result);
		return null;
	}

	@RequestMapping(value = "/delOperationLog")
	public String delOperationLog(HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam(value = "id", defaultValue = "") Long id) {
		JSONObject result = new JSONObject();
		boolean flag = true;
		this.operationLogService.deleteOperationLog(id);
		result.put("success", flag);
		PageJsonBean.printObject(response, result);
		return null;
	}

	@InitBinder
	protected void initBinder(HttpServletRequest request,
			ServletRequestDataBinder binder) throws Exception {
		DateFormat dateFormat = com.rfid.project.util.DateUtil.getFormat();
		dateFormat.setLenient(false);
		binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
	}

}
